/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.entity;

import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import towerz.game.entity.tower.TowerFactory;
import towerz.system.Utilities;
import towerz.system.xml.XmlNode;

/**
 *
 * @author LegACy
 */
public class BulletData {
	public static final int BULLET_DOWN			= 0;
	public static final int BULLET_DOWNLEFT		= 1;
	
	public static final int BULLET_LEFT			= 2;
	public static final int BULLET_RIGHT		= 3;

	public static final int BULLET_UPLEFT		= 4;
	public static final int BULLET_UPRIGHT		= 5;

	public static final int BULLET_UP			= 6;
	public static final int BULLET_DOWNRIGHT	= 7;

	protected BulletData() {
		//Initialize
		m_Buff		= 0;
		m_Area		= -1;
		m_Damage	= 0;
		m_Duration	= 200;
		m_Image		= null;

		rand = new Random();
	}

	public BulletData(XmlNode xml) {
		//Default
		this();

		//For each child
		for (int i = 0; i < xml.getChildren().size(); i++) {
			//Get variable
			String Tag		= ((XmlNode)xml.getChildren().elementAt(i)).getNodeName();
			String Value	= ((XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(0)).getNodeValue();

			//Check based on tag name
			if (Tag.equals("area"))				m_Area = Integer.parseInt(Value);
			else if (Tag.equals("buff"))		m_Buff = Integer.parseInt(Value);
			else if (Tag.equals("damage"))		m_Damage = Integer.parseInt(Value);
			else if (Tag.equals("duration"))	m_Duration = Integer.parseInt(Value);
			else if (Tag.equals("image")) {
				try {
					//Load image
					//m_Image = Image.createImage(Utilities.BULLET_FOLDER + "/" + Value + ".png");

					//Create image
					Image Source 	= Image.createImage(Utilities.BULLET_FOLDER + "/" + Value + ".png");
					if(Source.getWidth() == Source.getHeight()){
						m_Images = new Image[1];
						m_Images[0] = Source;
					} else {
						m_Images = new Image[8]; //peluru 8 arah mata angin

						//Slice it
						for (int y = 0; y < 1; y ++) { //Pasti hanya 1 baris
							for (int x = 0; x < 2; x++) { //Pasti hanya 2 kolom
								m_Images[(y * 2) + x] = Image.createImage(
										Source,
										x * Source.getWidth()/2,
										y * Source.getHeight(),
										Source.getWidth()/2,
										Source.getHeight(), 0);

								if(x == 0){
									m_Images[BULLET_LEFT]		= Image.createImage(m_Images[0], 0, 0, m_Images[0].getWidth(), m_Images[0].getHeight(), Sprite.TRANS_ROT90);
									m_Images[BULLET_UP]			= Image.createImage(m_Images[0], 0, 0, m_Images[0].getWidth(), m_Images[0].getHeight(), Sprite.TRANS_ROT180);
									m_Images[BULLET_RIGHT]		= Image.createImage(m_Images[0], 0, 0, m_Images[0].getWidth(), m_Images[0].getHeight(), Sprite.TRANS_ROT270);
								} else if (x == 1){
									m_Images[BULLET_UPLEFT]		= Image.createImage(m_Images[1], 0, 0, m_Images[1].getWidth(), m_Images[1].getHeight(), Sprite.TRANS_ROT90);
									m_Images[BULLET_UPRIGHT]	= Image.createImage(m_Images[1], 0, 0, m_Images[1].getWidth(), m_Images[1].getHeight(), Sprite.TRANS_ROT180);
									m_Images[BULLET_DOWNRIGHT]	= Image.createImage(m_Images[1], 0, 0, m_Images[1].getWidth(), m_Images[1].getHeight(), Sprite.TRANS_ROT270);
								}
							}
						}
					}
				} catch (IOException ex) {}
			}
		}
	}

	public int getArea()		{	return m_Area;		}
	public int getDamage()		{	return m_Damage;	}
	public Image getImage(int bulletDir)		{
		return m_Images[bulletDir];
	}
	public long getDuration()	{	return m_Duration;	}
	public XmlNode getBuffData() {
		return TowerFactory.instance().getBuffData(m_Buff);
	}

	//Data
	protected int	m_Buff;
	protected int	m_Area;
	protected int	m_Damage;
	protected long	m_Duration;
	protected Image m_Image;
	protected Image[] m_Images;

	protected Random rand;
}
